//
//  VC_Setting.m
//  yjw-winner
//
//  Created by Kae on 2018/8/31.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_Setting.h"
#import "VC_SettingIntroduce.h"
#import "VC_SettingResetPassword.h"
#import "VC_UserInfo.h"
#import "LogoutAlertView.h"


#define SETTING_IDENTIFIER @"identifier"
@interface VC_Setting ()<UITableViewDelegate, UITableViewDataSource> {
    UITableView *_tableView;
}
@property (nonatomic, strong) NSArray *cellArray;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) NSArray *sels;
@end

@implementation VC_Setting
+ (VC_Setting *)instance {
    VC_Setting *vc = (VC_Setting *)[[UIStoryboard storyboardWithName:@"MineStoryBoard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([VC_Setting class])];
    vc.title = @"设置";
    return vc;
}
- (NSArray *)sels {
    if (!_sels) {
        _sels = @[NSStringFromSelector(@selector(settingUserInfo:)),
                  NSStringFromSelector(@selector(settingWinnerUniversity:)),
                  NSStringFromSelector(@selector(settingResetPassword:))];
    }
    return _sels;
}
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 94.0f)];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 90.0f)];
        view.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:view];
        self.headImageView.bounds = CGRectMake(0, 0, 60, 60);
        self.headImageView.center = CGPointMake(view.bounds.size.width * 0.5, view.bounds.size.height * 0.5);
        self.headImageView.layer.cornerRadius = 30.0f;
        [view addSubview:self.headImageView];
    }
    return _headerView;
}
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.image = [UIImage imageNamed:@"un_audio_icon"];
    }
    return _headImageView;
}
- (NSArray *)cellArray {
    if (!_cellArray) {
        _cellArray = @[@{@"icon":@"setting_icon_userinfo", @"title":@"个人资料"},
                       @{@"icon":@"setting_icon_about", @"title":@"关于赢家大学"},
                       @{@"icon":@"setting_icon_code", @"title":@"密码重置"}];
    }
    return _cellArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = MJRefreshColor(242, 242, 242);
    _tableView.scrollEnabled = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [UIView new];
    _tableView.tableHeaderView = self.headerView;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SETTING_IDENTIFIER];
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.frame = CGRectMake(50, _tableView.bounds.size.height - 50 - 40 - 64 - 20, _tableView.bounds.size.width - 100, 40);
    [logoutButton setTitle:@"退出登陆" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    logoutButton.backgroundColor = MAIN_COLOR;
    [logoutButton addTarget:self action:@selector(logoutButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_tableView addSubview:logoutButton];
    logoutButton.layer.masksToBounds = YES;
    logoutButton.layer.cornerRadius = 4.0f;
}



#pragma mark --mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SETTING_IDENTIFIER forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.cellArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:dic[@"icon"]];
    cell.textLabel.text = dic[@"title"];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 49.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SEL sel = NSSelectorFromString(self.sels[indexPath.row]);
    if ([self respondsToSelector:sel]) {
        [self performSelector:sel withObject:nil afterDelay:0.0f];
    }
}

#pragma mark --mark EVENT

- (void)logoutButtonClick:(UIButton *)sender {
//    [MBProgressHUD showSuccess:@"退出登录"];
    
    UIView *maskView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    maskView.backgroundColor = [UIColor grayColor];
    maskView.alpha = 0.7;
    [self.view.window addSubview:maskView];
    
    LogoutAlertView *alertView = [LogoutAlertView alert];
    alertView.bounds = CGRectMake(0, 0, 285, 130);
    alertView.center = CGPointMake(self.view.mj_w * 0.5, self.view.mj_h - 200);
    [maskView addSubview:alertView];

    __weak typeof(self) weakself = self;
    __block LogoutAlertView *alert = alertView;
    __block UIView *mask = maskView;
    [alertView settingCallbackBlock:^(SettingButtonType type) {
        [alert removeFromSuperview];
        [mask removeFromSuperview];
        [weakself logoutButtonClickWith:type];
    }];
}
- (void)logoutButtonClickWith:(SettingButtonType)type {
    switch (type) {
        case SettingButtonTypeSure:
        {
            // 退出登录
            [self logout];
        }
            break;
        case SettingButtonTypeCancel:
        {
            // 取消
        }
            break;
        default:
            break;
    }
}
#pragma mark --mark SELS

/**
 个人资料

 @param info info
 */
- (void)settingUserInfo:(id)info {
    VC_UserInfo *vc = [[VC_UserInfo alloc] init];
    vc.title = @"个人资料";
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 关于赢家大学

 @param info info
 */
- (void)settingWinnerUniversity:(id)info {
    VC_SettingIntroduce *vc = [[VC_SettingIntroduce alloc] initWithNibName:NSStringFromClass([VC_SettingIntroduce class]) bundle:[NSBundle mainBundle]];
    vc.title = @"赢家大学";
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 密码重置

 @param info info
 */
- (void)settingResetPassword:(id)info {
    VC_SettingResetPassword *vc = [[VC_SettingResetPassword alloc] initWithNibName:NSStringFromClass([VC_SettingResetPassword class]) bundle:[NSBundle mainBundle]];
    vc.title = @"重置密码";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)logout {
    [self.navigationController popToRootViewControllerAnimated:NO];
    UINavigationController *na = (UINavigationController *)[[UIStoryboard storyboardWithName:@"LoginStoryBoard" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    [self presentViewController:na animated:NO completion:nil];
}

@end
